/***************************************************************************
 * 文件名：FMToolDetail
 * 功能：
 * 说明：
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/23 15:56:14
 * 创建人：葛浩
 * 邮箱：2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using System;
using System.Drawing;
using System.IO;
using System.Web.UI.Design.Directives;
using System.Windows.Forms;
using System.Windows.Shapes;
using Util;
using Path = System.IO.Path;

namespace AuxiliaryTools
{
	public partial class FMToolDetail : UserControl
	{
		#region 委托+事件

		//委托
		public delegate void EventHandler(object sender, EventArgs e);
		//事件
		public EventHandler OptionEvent;

		#endregion

		#region 属性

		/// <summary>
		/// 控件尺寸
		/// </summary>
		private Size _FormSize;
		public Size FormSize
		{
			get {  return _FormSize; }
			set { _FormSize = value; }
		}

		/// <summary>
		/// 已注册
		/// </summary>
		private bool _IsRegistered;
		public bool IsRegistered
		{
			get { return _IsRegistered; }
			set { _IsRegistered = value; }
		}

		/// <summary>
		/// 工具名称
		/// </summary>
		private string _ToolName;
		public string ToolName
		{
			get { return _ToolName; }
			set { _ToolName = value; }
		}

		/// <summary>
		/// 工具描述
		/// </summary>
		private string _ToolDesc;
		public string ToolDesc
		{
			get { return _ToolDesc; }
			set { _ToolDesc = value; }
		}

		/// <summary>
		/// 图片流
		/// </summary>
		private MemoryStream _MemoryStream;
		public MemoryStream MemoryStream
		{
			get { return _MemoryStream; }
			set { _MemoryStream = value; }
		}

		#endregion
		public FMToolDetail(Size size, string toolName, bool isRegistered = false, string toolDesc = "", MemoryStream memoryStream = null)
		{
			InitializeComponent();
			_FormSize = size;
			_ToolName = toolName;
			_IsRegistered = isRegistered;
			_ToolDesc = toolDesc;
			_MemoryStream = memoryStream;

			if (string.IsNullOrEmpty(_ToolName))
			{
				throw new Exception("工具名称不能为空！");
			}
		}

		private void FMToolDetail_Load(object sender, EventArgs e)
		{
			InitControl();
		}

		/// <summary>
		/// 初始化控件
		/// </summary>
	   private void InitControl()
		{
			try
			{
				this.Size = _FormSize;

				labFunctionName.Text = _ToolName;
				labFunctionDesc.Text = _ToolDesc;

				btnOption.Text = _IsRegistered ? "打开" : "下载";

				#region 处理图片资源

				//检测若存在图片文件流，则落地生成图片存至本地（DefaultImage文件夹下）
				string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, UtilConst.DefaultImage, (ToolName + ".png"));
				if (!File.Exists(imagePath))
				{
					//生成图片至small文件夹下
					string defauleImage = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, UtilConst.DefaultImage);
					if (!Directory.Exists(defauleImage))
					{
						Directory.CreateDirectory(defauleImage);
					}

					//存在默认图片
					//判断MemoryStream图片流文件流文件是否存在
					if (MemoryStream == null)
					{
						FileHelperUtil.GeneratorImage(imagePath, _ToolName);
					}
					else
					{
						//文件保存至DefaultImage中
						using (Image image = Image.FromStream(MemoryStream))
						{
							image.Save(imagePath);
						}
					}
				}

				pictureEdit1.Image = Image.FromFile(imagePath);
				pictureEdit1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
				#endregion

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 操作
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnOption_Click(object sender, EventArgs e)
		{
			OptionEvent?.Invoke(this, EventArgs.Empty);
		}
	}
}
